package com.poetize.plus.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.poetize.plus.entity.dto.ChangeForbiddenDTO;
import com.poetize.plus.entity.dto.UserDTO;
import com.poetize.plus.entity.dto.page.UserPageDTO;
import com.poetize.plus.entity.vo.UserPageVO;
import com.poetize.plus.entity.vo.UserVO;
import com.poetize.plus.service.UserService;
import com.poetize.plus.utils.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author zuosy
 * @created 2024/3/29 9:18
 */
@Tag(name = "用户信息")
@RestController
@RequestMapping("/admin/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @GetMapping("/userPage")
    @Operation(summary = "用户分页数据")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Page<UserPageVO>> userPage(UserPageDTO userPageDTO){
        return R.ok(userService.userPage(userPageDTO));
    }

    @PostMapping("/changeForbidden")
    @Operation(summary = "修改禁用状态")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> changeForbidden(@RequestBody @Validated ChangeForbiddenDTO changeForbiddenDTO){
        return R.ok(userService.changeForbidden(changeForbiddenDTO));
    }

    @GetMapping("/{id}")
    @Operation(summary = "用户详情")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<UserVO> queryUser(@PathVariable("id") Long id){
        return R.ok(userService.queryUser(id));
    }

    @PostMapping("/updateUser")
    @Operation(summary = "修改用户")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> updateUser(@RequestBody @Validated UserDTO userDTO){
        return R.ok(userService.updateUser(userDTO));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除用户")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> deleteUser(@PathVariable("id") Long id){
        return R.ok(userService.deleteUser(id));
    }
}
